<?php
require_once( 'mofachi.DataConnection.php' );
class BabyData {
	private $field = array();
	public function __get( $key ) {
		if( !isset( $this->field[ $key ] ) ) {
            return null;
        }
		return $this->field[ $key ];
	}
	public function __set( $key, $value ) {
		$this->field[ $key ] = $value;
	}
	public function __construct() {
		$this->field[ "baby_id" ] = null;
		$this->field[ "nickname" ] = null;
		$this->field[ "realname" ] = null;
		$this->field[ "gender" ] = null;
		$this->field[ "location_id" ] = null;
		$this->field[ "birthday" ] = null;
		$this->field[ "reg_time" ] = null;
		$this->field[ "edit_time" ] = null;
	}
	public function add() {
		$db = DataConnection::getConnection();
		if( $db == null ) return false;

		$query = "insert into babyinfo values( '', '".$this->field[ "nickname" ]."','".$this->field[ "realname" ];
		$query.= "','".$this->field[ "gender" ]."', '".$this->field[ "location_id" ];
		$query.= "','".$this->field[ "birthday" ]."',now(),now() )";
		if( !$db->query($query) ) {
			$db->close();
			return false;
		}
		$db->close();
		return true;
	}
	private function loadData( $row ) {
		foreach( $this->field as $key => $value ) {
			$this->field[ $key ] = $row[ $key ];
		}
	}
	public function loadByID() {
		$db = DataConnection::getConnection();
		if( $db == null ) return false;
		
		$query = "select * from babyinfo where baby_id=".$this->field[ "baby_id" ];
		$result = $db->query( $query );
		if( !$result || $result->num_rows < 1 ) {
			$db->close();
			return false;		
		}
		$this->loadData( $result->fetch_assoc() );

		$result->free();
		$db->close();
		return true;
	}
	public function loadByNickname() {
		$db = DataConnection::getConnection();
		if( $db == null ) return false;
		
		$query = "select * from babyinfo where nickname='".$this->field[ "nickname" ]."'";
		$result = $db->query( $query );
		if( !$result || $result->num_rows < 1 ) {
			$db->close();
			return false;
		}
		$this->loadData( $result->fetch_assoc() );

		$result->free();
		$db->close();
		return true;
	}
	public function update() {
		$db = DataConnection::getConnection();
		if( $db == null ) return false;
		
		$query = "update babyinfo set nickname='".$this->field[ "nickname" ];
		$query.= "', realname='".$this->field[ "realname" ]."', gender=".$this->field[ "gender" ];
		$query.= ", location_id=".$this->field[ "location_id" ].", birthday=date('";
		$query.= $this->field[ "birthday" ]."'), edit_time=now() where baby_id=".$this->field[ "baby_id" ];
		if( !$db->query($query) ) {
			$db->close();
			return false;
		}
		$db->close();
		return true;
	}
	public function remove() {
		$db = DataConnection::getConnection();
		if( $db == null ) return false;

		$db->query( "delete from babyinfo where baby_id=".$this->field[ "baby_id" ] );
		$db->close();
		return true;
	}
	//-------------------------------------------------------------------------------------
	// static functions
	//-------------------------------------------------------------------------------------
	public static function listData( $offset, $length ) {
		$db = DataConnection::getConnection();
		if( $db == null ) return false;

		$query = "select * from babyinfo order by baby_id desc limit ".$offset.",".$length;
		$result = $db->query( $query );
		if( !$result || $result->num_rows < 1 ) {
			$db->close();
			return false;
		}
		$returnArray = array();
		$num = $result->num_rows;
		for( $i = 0; $i < $num; $i++ ) {
			$user = new BabyData();
			$user->loadData( $result->fetch_assoc() );
			$returnArray[ $i ] = $user;
		}
		$result->free();
		$db->close();
		return $returnArray;
	}
}
?>